package com.example.ymm.modules1.service.impl.hl.kq;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.exception.YmException;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.hl.yw.YwApprove;
import com.example.ymm.modules1.entity.my.user.DbApprovalLog;
import com.example.ymm.modules1.entity.my.user.KqApproveLog;
import com.example.ymm.modules1.mapper.my.user.KqApproveLogMapper;
import com.example.ymm.modules1.query.kq.KqApproveLogQueryCriteria;
import com.example.ymm.modules1.service.hl.kq.KqApproveLogService;
import com.example.ymm.util.QueryHelpPlus;
import com.example.ymm.util.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
@DSTransactional
@DS("slave2")
@Slf4j
public class KqApproveLogServiceImpl extends BaseServiceImpl<KqApproveLogMapper, KqApproveLog> implements KqApproveLogService {



    @Override
    public TableList<KqApproveLog> list(KqApproveLogQueryCriteria criteria, YmPageDto pageDto) {
        IPage<KqApproveLog> page = new Page<KqApproveLog>(pageDto.getPage(),pageDto.getPageSize());
        QueryWrapper predicate = QueryHelpPlus.getPredicate(KqApproveLog.class, criteria);
        predicate.orderByDesc("create_time");
        baseMapper.selectPage(page, predicate);
        TableList<KqApproveLog> tableList=new TableList(page);
        return tableList;
    }

    public List<KqApproveLog> queryAll(KqApproveLogQueryCriteria criteria) {
        List<KqApproveLog> result =baseMapper.selectList(QueryHelpPlus.getPredicate(KqApproveLog.class, criteria));
        return result;
    }


    @Override
    public DbApprovalLog approveCount(KqApproveLogQueryCriteria criteria) {
        if(UtilValidate.isEmpty(criteria.getAddDate())){
            throw new YmException("考勤日期不能为空");
        }
        criteria.setUserId(getUserId());
        return baseMapper.approveCount(criteria);
    }

    @Override
    public KqApproveLog getUserIdAndAddDate(String userId, String addDate) {
        KqApproveLogQueryCriteria criteria = new KqApproveLogQueryCriteria();
        criteria.setUserId(userId);
        criteria.setAddDate(addDate);
        List<KqApproveLog> kqApproveLogs = queryAll(criteria);
        if(kqApproveLogs.size()>0){
            return kqApproveLogs.get(0);
        }
        return null;
    }

    @Override
    public List<KqApproveLog> getXiuxiList(KqApproveLogQueryCriteria criteria) {
        return baseMapper.getXiuxiList(criteria);
    }
}
